package com.feng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.domain.ResponseResult;
import com.feng.domain.dto.TagListDto;
import com.feng.domain.dto.UpdateTagDto;
import com.feng.domain.vo.ArticleListPageVo;
import com.feng.domain.vo.TagAllListVo;
import com.feng.domain.vo.TagListVo;
import com.feng.enums.AppHttpCodeEnum;
import com.feng.mapper.TagMapper;
import com.feng.domain.entity.Tag;
import com.feng.service.TagService;
import com.feng.utils.BeanCopyUtil;
import com.feng.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2023-04-19 16:10:49
 */
@Service()
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Override
    public ResponseResult tagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(tagListDto.getName()), Tag::getName, tagListDto.getName());
        wrapper.like(StringUtils.hasText(tagListDto.getRemark()), Tag::getRemark, tagListDto.getRemark());

        Page<Tag> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page, wrapper);
        List<TagListVo> tagListVos = BeanCopyUtil.articleListVo(page.getRecords(), TagListVo.class);
        ArticleListPageVo pageVo = new ArticleListPageVo(tagListVos, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addTag(TagListDto tagListDto) {
        Tag tag = BeanCopyUtil.articleVo(tagListDto, Tag.class);
        save(tag);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delTag(List<Long> idList) {
        if(idList.isEmpty()){
            return ResponseResult.errorResult(AppHttpCodeEnum.OPTION_NOT_NULL);
        }
        getBaseMapper().deleteBatchIds(idList);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getTag(Long id) {
        Tag tag = getById(id);
        TagListVo tagVo = BeanCopyUtil.articleVo(tag, TagListVo.class);
        return ResponseResult.okResult(tagVo);
    }

    @Override
    public ResponseResult updateTag(UpdateTagDto tagDto) {
        Tag tag = BeanCopyUtil.articleVo(tagDto, Tag.class);
        updateById(tag);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listAllTag() {
        List<Tag> tags = list();
        List<TagAllListVo> tagAllListVos = BeanCopyUtil.articleListVo(tags, TagAllListVo.class);
        return ResponseResult.okResult(tagAllListVos);
    }
}

